if(length(tryCatch(list.dirs(path="~/Library/CloudStorage/Dropbox/Robots and Trade in Services"),error=function(e) e))==0){
  base.path<-"~/Dropbox/Robots and Trade in Services/"
} else {
  base.path<-"~/Library/CloudStorage/Dropbox/Robots and Trade in Services/"
}

#Load data and functions
source(paste0(base.path,"Drafts/Replication/03 Functions and Processing.R"))

lucid.proc$policy_difference <- ifelse(lucid.proc$treatment_automation_labor=="Labor",lucid.proc$restrict_imports_difference,lucid.proc$restrict_automation_difference)

###########################
## Table 5 (Appendix B) ##
###########################


acs<-read.csv(paste0(base.path,"Data/ACS/ACSDP5Y2020.DP05-2022-06-21T213605.csv"),stringsAsFactors = FALSE) %>%
  filter(row_number() %in% c(10,11,12,13,14,15,16,17,18,28,67,68,75)) %>%
  mutate(
    Label..Grouping.=stringr::str_trim(Label..Grouping.),
    value=as.numeric(stringr::str_replace_all(United.States..Estimate,",","")),
    frac_reported=as.numeric(stringr::str_replace(United.States..Percent,",|%",""))/100
  ) %>%
  mutate(Label..Grouping.=case_when(
    Label..Grouping.=="Hispanic or Latino (of any race)" ~ "Hispanic",
    Label..Grouping.=="Black or African American" ~ "Black",
    TRUE ~ Label..Grouping.
  )) %>%
  mutate(
    Group=case_when(
      Label..Grouping.=="20 to 24 years" ~ "20 to 34 years of age",
      Label..Grouping.=="25 to 34 years" ~ "20 to 34 years of age",
      Label..Grouping.=="35 to 44 years" ~ "35 to 54 years of age",
      Label..Grouping.=="45 to 54 years" ~ "35 to 54 years of age",
      Label..Grouping.=="50 to 54 years" ~ "55 to 64 years of age",
      Label..Grouping.=="55 to 59 years" ~ "55 to 64 years of age",
      Label..Grouping.=="60 to 64 years" ~ "55 to 64 years of age",
      Label..Grouping.=="65 to 74 years" ~ "65 years of age and over",
      Label..Grouping.=="75 to 84 years" ~ "65 years of age and over",
      Label..Grouping.=="85 years and over" ~ "65 years of age and over",
      TRUE ~ Label..Grouping.
    )) %>%
  group_by(stringr::str_detect(Group,"year")) %>%
  mutate(total_pop=sum(value)) %>%
  ungroup() %>%
  group_by(Group) %>%
  summarize(
    `ACS Percentage`=case_when(stringr::str_detect(Group,"year") ~ sum(value)/total_pop,TRUE ~ frac_reported)
  ) %>% 
  ungroup() %>%
  distinct() %>%
  mutate(`ACS Percentage`=scales::label_percent(accuracy=0.01)(`ACS Percentage`))

#important note: the breakdowns by race are not available by age, so the race and ethnicity values reflect the overall population (but the survey reflects only 18+)

lucid.proc %>% 
  summarize(
    `Female`=mean(gender_str=="Female",na.rm=TRUE),
    `20 to 34 years of age`=mean(between(age,20,34),na.rm=TRUE),
    `35 to 54 years of age`=mean(between(age,35,54),na.rm=TRUE),
    `55 to 64 years of age`=mean(between(age,55,64),na.rm=TRUE),
    `65 years of age and over`=mean(between(age,65,150),na.rm=TRUE),
    `Hispanic`=mean(hispanicbin,na.rm=TRUE),
    `White`=mean(white,na.rm=TRUE),
    `Black`=mean(black,na.rm=TRUE)
  ) %>%
  reshape2::melt() %>%
  mutate(value=scales::label_percent(accuracy=0.01)(value)) %>%
  rename(Group=variable,`Sample Percentage`=value) %>%
  left_join(acs) %>%
  xtable::xtable(label="tab:compare",caption="Sample Comparison with American Communities Survey (5 year sample from 2020)") %>%
  xtable::print.xtable(comment = FALSE,include.rownames = FALSE)


